package com.atguigu.gmall.order.consumer;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.temporal.ValueRange;
import java.util.Map;

/**
 * 监听支付结果的消费者
 */
@Component
public class OrderUpdateConsumer {

    @Autowired
    private OrderService orderService;

    /**
     * 监听订单支付结果的消费者的实现方法
     *
     */
    @RabbitListener(queues = "order_pay_queue")
    public void updateOrder(String msg){
        //进行转换
        Map<String,String> map = JSONObject.parseObject(msg, Map.class);
        //进行解析，获取请求的结果
        String returnCode = map.get("return_code");
        if (returnCode.equals("SUCCESS")){
            //获取交易的结果
            String resultCode = map.get("result_code");
            if (resultCode.equals("SUCCESS")){
                //支付成功，修改订单的状态
                orderService.paySuccess(map);
                return;
            }
        }
        //支付失败，修改订单的状态
        orderService.payFail(map);
    }
}
